package com.woniuxy.goods.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.woniuxy.core.annotation.Permission;
import com.woniuxy.core.exception.ParamException;
import com.woniuxy.core.util.Result;
import com.woniuxy.goods.dto.AppraisalRecordsDto;
import com.woniuxy.goods.entity.GoodsAppraisalRecords;
import com.woniuxy.goods.form.AddAppraisalForm;
import com.woniuxy.goods.form.FindRecordForm;
import com.woniuxy.goods.param.AddAppraisalParam;
import com.woniuxy.goods.param.FindRecordParam;
import com.woniuxy.goods.service.GoodsAppraisalRecordsService;
import com.woniuxy.goods.util.TokenUtil;
import io.swagger.annotations.*;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 * 鉴定记录控制层接口
 * @author ...
 * @since 2024年05月22日
 */
@RestController
@Api(tags = "鉴定记录控制层接口")
@RequestMapping("/goodsAppraisalRecords")
public class GoodsAppraisalRecordsController {
    /**
     * 鉴定记录服务层接口
     */
    @Resource
    private GoodsAppraisalRecordsService appraisalService;
    /**
     * 解析token工具类
     */
    @Resource
    private TokenUtil tokenUtil;

    /**
     * 新增鉴定记录
     * @param form 新增鉴定记录对象
     * @param token 携带当前登录管理员id的请求头
     * @return 鉴定记录
     * @throws Exception
     */
    @ApiOperation("新增鉴定记录")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = GoodsAppraisalRecords.class)
    })
    @PostMapping("/addAppraisal")
    @Permission(roles = {"鉴定师"})
    public Result addAppraisal(@Validated @RequestBody AddAppraisalForm form,BindingResult br, @RequestHeader String token) throws Exception{
        if(br.hasErrors()) throw new ParamException(10001,"参数有误");
        Integer accountId = tokenUtil.getAccountId(token);
        AddAppraisalParam param = BeanUtil.toBean(form, AddAppraisalParam.class);
        param.setAccountId(accountId);
        GoodsAppraisalRecords records = appraisalService.addAppraisal(param);
        return Result.success("新增鉴定记录成功",records);
    }

    /**
     * 根据物品id查询历史鉴定记录
     * @param goodsId 物品id
     * @return 鉴定记录
     * @throws Exception
     */
    @ApiOperation("根据物品id查询历史鉴定记录")
    @ApiImplicitParam("物品id")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = GoodsAppraisalRecords.class)
    })
    @GetMapping("/findExecuteByGoodsId")
    public Result findExecuteByGoodsId(Integer goodsId) throws Exception{
        List<GoodsAppraisalRecords> records = appraisalService.findExecuteByGoodsId(goodsId);
        return Result.success("根据物品id查询历史鉴定记录成功",records);
    }

    /**
     * 根据物品id查询最新的鉴定记录及历史记录
     * @param goodsId 物品id
     * @return 鉴定记录
     * @throws Exception
     */
    @ApiOperation("根据物品id查询最新的鉴定记录及历史记录")
    @ApiImplicitParam("物品id")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = GoodsAppraisalRecords.class)
    })
    @GetMapping("/findNewExecuteByGoodsId")
    public Result findNewExecuteByGoodsId(Integer goodsId) throws Exception{
        AppraisalRecordsDto records = appraisalService.findNewExecuteByGoodsId(goodsId);
        return Result.success("根据物品id查询最新的鉴定记录及历史记录成功",records);
    }

    /**
     * 查询所有最新的鉴定记录及历史记录
     * @param form 查询记录参数对象
     * @return 鉴定记录集合
     * @throws Exception
     */
    @ApiOperation("查询所有最新的鉴定记录及历史记录")
    @ApiImplicitParam("查询记录参数对象")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = GoodsAppraisalRecords.class)
    })
    @PostMapping("/findNewExecutes")
    public Result findNewExecutes(@RequestBody FindRecordForm form) throws Exception{
        IPage<AppraisalRecordsDto> records = appraisalService.findNewExecutes(BeanUtil.toBean(form, FindRecordParam.class));
        return Result.success("查询所有最新的鉴定记录及历史记录成功",records);
    }

}

